﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.Status;
using System.Windows.Forms;

namespace Handrise.CDCable.Mobile.Business
{
    /// <summary>
    /// 网络服务，用于监控网络的变化（USB、APN、无网络）
    /// </summary>
    public class NetService
    {
        private NetType currentNetType = NetType.None;

        private Timer timerGetNetType ;

        public event NetChangeHandler OnNetChange;

        public NetService()
        {
            timerGetNetType = new Timer();
            timerGetNetType.Interval = 5000;
            timerGetNetType.Tick += new EventHandler(timerGetNetType_Tick);
            timerGetNetType.Enabled = true;
        }

        void timerGetNetType_Tick(object sender, EventArgs e)
        {
            NetType netType = CurrentNetType;
            if (netType != currentNetType)
            {
                currentNetType = netType;
                if (OnNetChange != null)
                    OnNetChange(currentNetType);
            }
        }

        public NetType CurrentNetType
        {
            get 
            {
                if (SystemState.CradlePresent)
                {
                    return NetType.USB;
                }
                else if (SystemState.ConnectionsCellularCount == 1)
                {
                    return NetType.APN;
                }
                else
                {
                    return NetType.None;
                }
            }
        }
    }

    public enum NetType{USB,APN,None};
    public delegate void NetChangeHandler(NetType newNetType);
}
